package redis

import (
	"context"
	"sync"

	"github.com/go-redis/redis/v8"

	"github.com/super-team/start-kit/configs"
)

var (
	Conn *redis.Client
	once sync.Once
)

func InitRedis() {
	cfg := configs.Config.Redis
	once.Do(func() {
		client := redis.NewClient(&redis.Options{
			Addr:         cfg.URL,
			Password:     cfg.Password,
			DB:           cfg.DB,
			PoolSize:     cfg.PoolSize,
			MinIdleConns: cfg.MinIdleConns,
			MaxRetries:   cfg.MaxRetries,
		})
		_, err := client.Ping(context.Background()).Result()
		if err != nil {
			panic(err)
		}
		Conn = client
	})
}

func Close() {
	if Conn != nil {
		Conn.Close()
	}
}
